# -*- coding: utf-8 -*-
# -----------------------------------------
# @Time       : 2018/6/18 9:31
# @Author     : dqz 
# @Email      : 1406990390@qq.com
# @File       : sh_tools.py
# @Software   : PyCharm
# -----------------------------------------
"""
src: 对比的参考库表
    username:
    password:
    host:
    port:
    database:
    table:

dest: 需要对比的库表
    username:
    password:
    host:
    port:
    database:
    table:
"""

import subprocess

from utils.decorator import check_conn, check_table_key
from utils.log import log


@check_table_key
@check_conn
def get_diff_sql(src, dest):
    infos = {
        "status": False,
        "data": "",
        "msg": ""
    }
    try:
        diff_sql = (f"""mysqldiff --server1={src["username"]}:{src["password"]}@{src["host"]}:{src["port"]} """
                    f"""--server2={dest["username"]}:{dest["password"]}@{dest["host"]}:{dest["port"]} """
                    f"""\`{src['database']}\`.\`{src['table']}\`:\`{dest['database']}\`.\`{dest['table']}\` """
                    """--changes-for=server2 --skip-table-options -d sql""")
        log.info(diff_sql)
        status, diffsql = subprocess.getstatusoutput(diff_sql)
        if status:
            infos["data"] = diffsql
            infos["status"] = True

        infos["msg"] = "Success. All objects are the same"

    except Exception as e:
        infos["msg"] = str(e)

    return infos
